$(document).ready(function () {
    //修改排序
    $("#mdFileList input[name='sort']").change(function () {
        let sort = $(this).val()
        let id = $(this).parent().parent().attr("data-id")
        let fileName = $(this).attr("fileName")
        $.post('/admin/sort', {id: id, fileName: fileName, sort: sort}, function (data) {
            console.log(data)
            if (data.code === 0) {
                location.reload();
            } else {
                alert(data.msg)
            }
        })
    });

    //重命名
    $("#mdFileList input[name='fileName']").change(function () {
        let newFileName = $(this).val()
        $.post('/admin/rename', {id: $(this).parent().parent().attr("data-id"), newFileName: newFileName}, function (data) {
            console.log(data)
            if (data.code === 0) {
                location.reload();
            } else {
                alert(data.msg)
            }
        })
    });

    //删除
    $("#mdFileList .md-status").on("click", function () {
        let fileName = $(this).attr("fileName")
        $.post('/admin/delete', {fileName: fileName}, function (data) {
            console.log(data)
            if (data.code === 0) {
                location.reload();
            } else {
                alert(data.msg)
            }
        })
    });

    // add
    $("#addFileBtn").on("click", function () {
        let data = {
            title: $('#cons-title').val(),
            isDir: $('#isDir').val(),
            sortNum: $('#sort-number').val(),
            categoryId: $('#categoryId').val()
        }
        if (data.fileName == "") {
            return false
        }
        $.post("/admin/add-file", data, function (data) {
            if (data.code === 0) {
                location.href = "/";
            } else {
                alert(data.msg)
            }
        });
    })

    $("#dir-addBtn").bind("click", function () {
        let data = {
            dirName: $("#dir-newName").val(),
            sortNum: $("#dir-sortNumber").val()
        }
        if (data.dirName == "") {
            return false
        }
        $.post("/admin/add-dir", data, function (data) {
            if (data.code === 0) {
                location.href = "/";
            } else {
                alert(data.msg)
            }
        });
    })
});